package com.example.api2interface.config;

import com.example.api2interface.filter.SignFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 过滤器配置
 */
@Configuration
public class SignFilterConfig {
    @Value("${sign.maxTime}")
    private String signMaxTime;

    // 为了给filter中的初始化参数赋值
    private Map<String, String> initParametersMap = new HashMap<>();

    @Bean
    public SignFilter getSignFilterBean() {
        return new SignFilter();
    }

    @Bean
    public FilterRegistrationBean<SignFilter> signFilterBean() {
        // 设置初始化参数
        initParametersMap.put("signMaxTime", signMaxTime);
        FilterRegistrationBean<SignFilter> registration = new FilterRegistrationBean<>();
        // registration.setFilter(new SignFilter());
        // 或者将过滤器类搞成bean
        registration.setFilter(getSignFilterBean());
        registration.setName("SignFilter");
        registration.setInitParameters(initParametersMap);
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        return registration;
    }




}
